<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$tblname = $_REQUEST["tblname"];

if ($tblname == "")
    $tblname = "tbl_pet";
$db = NEW myDBS();

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net, account_no "
                . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
                . " WHERE payment_method = 'transfer' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id ";

        break;

    case "tbl_petition":

        $sql = "SELECT DISTINCT p_id AS pet_id, expenses1_name, expenses2_name, inpNet AS net, account_no "
                . " FROM tbl_petition "
                . " LEFT JOIN expenses_lvl1 ON ex1_id = expenses_lvl1.expenses1_id "
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
                . " WHERE payment_method =  'transfer' ";

        break;

    case "tbl_salary":

        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, salary AS net, tbl_payment.account_no, bank.bank_name, branch.branch_name "
                . " FROM tbl_salary"
                . " LEFT JOIN expenses_lvl1 ON ex1_id = expenses_lvl1.expenses1_id "
                . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id, tbl_payment, bank, branch "
                . " WHERE tbl_payment.payment_method =  'trans'"
                . " AND tbl_salary.id = tbl_payment.card_id "
                . " AND approved_status =  'อนุมัติ' "
                . " AND bank.bank_id = tbl_payment.bank_id "
                . " AND branch.branch_id = tbl_payment.branch_id ";

        break;
    case "tbl_welfare":

        $sql = "SELECT pet_id, expenses1_name, expenses2_name, cheque_no, sum(welfare) AS net "
                . " FROM tbl_welfare, tbl_payment, expenses_lvl1, expenses_lvl2 "
                . " WHERE approved_status = 'อนุมัติ' "
                . " AND tbl_welfare.id = tbl_payment.card_id "
                . " AND payment_method = 'trans' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id "
                . " GROUP BY pet_id, expenses1_name, expenses2_name, cheque_no ";
        break;
    default:
//        $sql = "SHOW TABLES";
        $sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net, account_no "
                . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
                . " WHERE payment_method = 'transfer' "
                . " AND expenses_lvl1.expenses1_id = ex1_id "
                . " AND expenses_lvl2.expenses2_id = ex2_id ";

        break;
}
//$sql = "SELECT DISTINCT pet_id, expenses1_name, expenses2_name, net "
//        . " FROM tbl_pet, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id "
//        . " UNION ALL "
//        . "SELECT DISTINCT p_id AS pet_id, expenses1_name, expenses2_name, inpNet AS net"
//        . " FROM tbl_petition, expenses_lvl1, expenses_lvl2 "
//        . " WHERE payment_method = 'cheque' "
//        . " AND expenses_lvl1.expenses1_id = ex1_id "
//        . " AND expenses_lvl2.expenses2_id = ex2_id ";
//echo $sql;
$result = $db->runSQL($sql);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();

        $("#tblname").change(function() {
            //alert($(this).val());
            window.location.href = "index.php?pages=trans&tblname=" + $(this).val();
        });
    });

    function printTrans(id) {
        var val = [];
        var count = 0;
        var tblname = "<?php echo $tblname; ?>";

        $(':checkbox:checked').each(function(i) {
            val[i] = $(this).val();
            count++;
            //alert(val[i]);
        });

        if (count == 0) {
            alert("กรุณาเลือกข้อมูลที่ต้องการบันทึกด้วยครับ");
        } else {
            $('<div>').dialog2({
                autoOpen: true,
                title: "พิมพ์ใบโอนเงิน",
                content: "dlg_trans.php?val=" + val + "&tblname=" + tblname,
                id: "modal-print"
            });
        }
    }
</script>

<div class="control-group">
    <div class="controls-row">
        <select id="tblname" name="tblname" >
            <option value="">เลือก...</option>
            <option value="tbl_pet" <?php echo ($tblname == "tbl_pet") ? "selected" : ""; ?>>อื่นๆ</option>
            <option value="tbl_petition" <?php echo ($tblname == "tbl_petition") ? "selected" : ""; ?>>โครงการ</option>
            <option value="tbl_salary" <?php echo ($tblname == "tbl_salary") ? "selected" : ""; ?>>เงินเดือน</option>
            <option value="tbl_welfare" <?php echo ($tblname == "tbl_welfare") ? "selected" : "";            ?>>สวัสดิการ</option>
        </select>
        <?php
        if ($tblname <> "tbl_salary") {
            ?>
            <a href="javascript:printTrans()" class=" btn btn-primary" target="_blank"><i class="icon-print icon-white"></i> บันทึกเงินโอน</a>
        <?php } ?>
    </div>
</div>

<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>#</th>
            <th>เลขที่ฎีกา</th>
            <?php
            if ($tblname <> "tbl_salary") {
                ?>
                <th>หมวด</th>
                <?php
            }
            ?>
            <th>ประเภท</th>
            <th>จำนวนเงิน</th>
            <?php
            if ($tblname == "tbl_salary") {
                ?>
                <th>ธนาคาร</th>
                <th>สาขา</th>
                <th>action</th>
            <?php } else {
                ?>
                <th>action</th>
                <?php
            }
            ?>


        </tr>
    </thead>
    <tbody>
        <?php
        $i = 1;
        while ($rows = $db->fetch_object($result)) {
            ?>
            <tr>
                <td>&nbsp;<?php echo $i; ?></td>
                <td>&nbsp;<?php echo $rows->pet_id; ?></td>
                <?php
                if ($tblname <> "tbl_salary") {
                    ?>
                    <td>&nbsp;<?php echo $rows->expenses1_name; ?></td>
                <?php } ?>
                <td>&nbsp;<?php echo $rows->expenses2_name; ?></td>
                <td><p class="text-right">&nbsp;<?php echo number_format($rows->net, 2); ?></p></td>

                <?php
                if ($tblname == "tbl_salary") {
                    ?>
                    <td><?php echo $rows->bank_name; ?></td>
                    <td><?php echo $rows->branch_name; ?></td>
                    <td>&nbsp;<a href="trans_print.php?account_no=<?php echo $rows->account_no; ?>&tblname=<?php echo $tblname; ?>" target="_blank"><i class="icon-print"></i></a></td>
                    <?php
                } else {

                    if ($rows->account_no == "") {
                        ?>
                        <td><p class="text-center">&nbsp;<input type='checkbox' name='check[]' id='check_//<?php echo $rows->pet_id; ?>' value='<?php echo $rows->pet_id; ?>' /></p></td>
                        <?php
                    } else {
                        ?>
                        <td><p class="text-center">&nbsp;ทำรายการแล้ว</p></td>
                        <?php
                    }
                }
                ?>
            </tr>
            <?php
            $i++;
        }
        ?>
    </tbody>
</table>

